<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /*
    
    */
    var coinChange = function (coins, amount) {
      // 最多硬币个数为，coins=[1]时，amount个1
      // 所以fill amount+1 即可
      const dp = new Array(amount + 1).fill(amount + 1);
      // dp[0] = 0
      // dp[i] = Math.min(dp[i-coin]) + 1, 0<=i-coin<i
      dp[0] = 0;
      for (let i = 1; i <= amount; i++) {
        for (const coin of coins) {
          // 如果当前的硬币值大于总金额，这个硬币不可用，直接判断下一个硬币
          if (coin > i) {
            continue;
          }
          // 如果当前硬币值小于等于总金额，则在dp[i-coin]的硬币数量情况上加1，并且选最小的那个dp[i-coin]+1
          dp[i] = Math.min(dp[i], dp[i - coin] + 1);
        }
      }
      return dp[amount] > amount ? -1 : dp[amount];
    };
    console.log(coinChange(coins = [1, 2, 5], amount = 11))
  </script>
</body>

</html>